<template>
  <div>
    <div>【服务资源占用比】</div>
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})


const target = ref(null)
let myChart = null
const render = () => {
  const option = {
    grid: {
      top: 10,
      left: -10,
      rigtht: -20,
      bottom: 10,
      containLabel: true
    },
    xAxis: {
      data: props.data.servers.map((item) => item.name),
	  
    },
    yAxis: {
      show: false,
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      }
    },
    series: {
      type: 'bar',
      max: function (value) {
        // 取整
        return parseInt(value.max * 1.2)
      },
      data: props.data.servers.map((item) => item.value),
      barWidth: 15,
      barCategoryGap: '30%',
      itemStyle: {
        barBorderRadius: 20
      },

      label: {
        show: true,
        position: 'top',
        color: 'white',
        formatter: '{c}%'
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    }
  }
  myChart.setOption(option)
}
onMounted(() => {
  myChart = echarts.init(target.value)
  render()
})
watch(
  () => props.data,
  (newValue) => {
    render()
  }
)
</script>

<style lang="scss" scoped></style>
